<template>
  <el-form :model="dataForm" :rules="rules" class="form-box" label-position="top" ref="dataFormRef"
    @keyup.enter="dataFormSubmitHandle()" label-width="120px">
    <el-row :gutter="20">
      <el-col :span="8">
        <el-form-item prop="name" label="班级">
          <el-input v-model="dataForm.name" placeholder="请输入"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item prop="gradeId" label="年级">
          <el-select v-model="dataForm.gradeId" clearable placeholder="请选择">
            <el-option :value="item.id" :label="item.name" v-for="(item, index) in gradeList" :key="index"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-form-item prop="owner" label="负责人">
          <el-input v-model="dataForm.owner" placeholder="请输入"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item prop="ownerTel" label="联系方式">
          <el-input v-model="dataForm.ownerTel" placeholder="请输入"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item prop="level" label="排序">
      <el-input-number v-model="dataForm.level" controls-position="right" :min="0" label="排序"></el-input-number>
    </el-form-item>
    <el-form-item class="footer-btn">
      <el-button @click="cancelHandle()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { useRoute, useRouter } from "vue-router";

const route = useRoute();
const router = useRouter();
const id = route.query?.id;
const dataFormRef = ref();
const gradeList = ref<any[]>([]);

const dataForm = reactive({
  id: "",
  name: "",
  gradeId: "",
  owner: "",
  ownerTel: "",
  level: 0
});

const rules = ref({
  name: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  gradeId: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
});

const init = () => {
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  Promise.all([getGradeList()]).then(() => {
    if (id) {
      getInfo(`${id}`);
    }
  });
};

// 获取信息
const getInfo = (id: string) => {
  baseService.get(`/study/class/${id}`).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 获取年级
const getGradeList = () => {
  return baseService.get("/study/grade/page").then((res) => {
    if (res.code !== 0) {
      return ElMessage.error(res.msg);
    }
    gradeList.value = res.data.list;
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/study/class", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          router.push('/study/class');
        }
      });
    });
  });
};

const cancelHandle = () => {
  router.go(-1);
};

onMounted(() => {
  init();
})
</script>

<style lang="less" scoped>
.mod-sys__dept {
  .dept-list {

    .el-input__inner,
    .el-input__suffix {
      cursor: pointer;
    }
  }
}
</style>
